package com.hk.web.controller.api.backend;

import com.hk.commons.JsonResult;
import com.hk.commons.ProcessAPI;
import com.hk.commons.enums.ArticleType;
import com.hk.commons.utils.CglibBeanUtils;
import com.hk.web.context.ArticleContext;
import com.hk.web.controller.Routes;
import com.hk.web.controller.api.APIController;
import com.hk.web.domain.entity.Article;
import com.hk.web.dto.article.ArticleDTO;
import com.hk.web.dto.article.ArticleQueryDTO;
import com.hk.web.dto.article.ArticleUpdateDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author Antinomy
 * @date 2016-11-16
 */

@Api(value = "官网文章模块-后端接口", description = "官网文章模块-后端接口")
@RestController
public class ArticleBackendAPIController extends APIController {

    private Logger logger = LoggerFactory.getLogger(ArticleBackendAPIController.class);

    @Autowired
    private ArticleContext articleContext;

    @ApiOperation(value = "创建文章", notes = "创建文章", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.ARTICLE, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult create(HttpServletRequest request, @RequestBody Article article) {

        ProcessAPI api = (jsonResult, validate) -> {
            validate.checkAndPrepare("title", article.getTitle());

            articleContext.storeNewActicle(article);

            jsonResult.result = article.getTitle();
        };

        return api.process(request);
    }


    @ApiOperation(value = "更新文章", notes = "更新文章", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.ARTICLE_UPDATE, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult update(HttpServletRequest request, @RequestBody ArticleUpdateDTO dto, @PathVariable String id) {

        ProcessAPI api = (jsonResult, validate) -> {

            Long artiId = validate.checkAndPrepareLong("id", id);
            Article article = articleContext.getArticleById(artiId);

            CglibBeanUtils.copy(article, dto);

            articleContext.updatePubishDate(article);

            articleContext.store(article);

            jsonResult.result = article.getTitle();
        };

        return api.process(request);
    }


    @ApiOperation(value = "文章列表", notes = "文章列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.ARTICLE_TYPE_LIST_ALL, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listAll(HttpServletRequest request, @PathVariable String type) {

        ProcessAPI api = (jsonResult, validate) -> {
            String typeTxt = validate.checkAndPrepare("type", type);
            ArticleType articleTypetype = ArticleType.valueOf(typeTxt);

            List<Article> articles = articleContext.getArticlesByType(articleTypetype);
            List<HashMap> result = articleContext.renderDtos(articles);
            jsonResult.result = result;
            jsonResult.size = (long) articles.size();
        };

        return api.process(request);
    }


    @ApiOperation(value = "文章分页列表", notes = "文章分页列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.ARTICLE_LIST_ALL, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult pageAll(HttpServletRequest request, ArticleQueryDTO dto) {

        ProcessAPI api = (jsonResult, validate) -> {

            validate.check("curPage", dto.getCurPage());
            validate.check("pageSize", dto.getPageSize());

            Page<Article> articles = articleContext.getArticlesByPage(dto);
            List<HashMap> result = articleContext.renderDtos(articles.getContent());
            jsonResult.result = result;
            jsonResult.size = articles.getTotalElements();
        };

        return api.process(request);
    }


    @ApiOperation(value = "后端查看文章详情", notes = "后端查看文章详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.ARTICLE_DETAIL_UUID, method = RequestMethod.GET)
    @ResponseBody
    public ArticleDTO detailByUuid(HttpServletRequest request, @PathVariable String uuid) {
        ArticleDTO dto = new ArticleDTO();

        ProcessAPI api = (jsonResult, validate) -> {
            validate.checkAndPrepare("uuid", uuid);

            Article article = articleContext.getArticleByUuid(uuid);

            CglibBeanUtils.copy(dto, article);

            jsonResult.result = dto;
        };

        api.process(request);

        return dto;
    }

    @ApiOperation(value = "后端修改文章置顶", notes = "后端修改文章置顶", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.API_ARTICLE_UUID_CHANGE_ON_TOP, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult changeOnTopByUuid(HttpServletRequest request, @PathVariable String uuid) {

        ProcessAPI api = (jsonResult, validate) -> {
            validate.checkAndPrepare("uuid", uuid);

            Article article = articleContext.changeOnTopByUuid(uuid);

            jsonResult.result = articleContext.renderActicle(article);
        };

       return api.process(request);
    }

    @ApiOperation(value = "后端删除文章", notes = "后端删除文章", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.API_ARTICLE_UUID_DELETE, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deleteByUuid(HttpServletRequest request, @PathVariable String uuid) {

        ProcessAPI api = (jsonResult, validate) -> {
            validate.checkAndPrepare("uuid", uuid);

            Article article = articleContext.deleteByUuid(uuid);

            jsonResult.result = articleContext.renderActicle(article);
        };

        return api.process(request);
    }


}
